import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/** Class that allows resizing images and writing them to specified files.
 *  Code is based on the code from http://www.rgagnon.com/howto.html website
 * 
 * @author crezax
 *
 */

public class ScaleJPG {
	/**
	 * Static method, used to resize image and to write it to the specified file.
	 * 
	 * @param bufferedImage Image to be resized.
	 * @param width The width of image after resizing.
	 * @param height The height of image after resizing.
	 * @param dest Destination path for file the image should be written to
	 * @throws IOException If writing image to the file fails.
	 */
	public static void scale(BufferedImage bufferedImage, int width, int height, String dest)
	throws IOException {
		BufferedImage bsrc = bufferedImage;
		BufferedImage bdest =
			new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = bdest.createGraphics();
		AffineTransform at =
			AffineTransform.getScaleInstance((double)width/bsrc.getWidth(),
					(double)height/bsrc.getHeight());
		g.drawRenderedImage(bsrc,at);
		ImageIO.write(bdest,"JPG",new File(dest));
	}
}